"""
Test Friends module 
"""

from django.test import TestCase
from models import RSMUser, User
#from django.template import RequestContext
from RSM.my_friends.views import getUserNames

getUserNames = getUserNames

class FriendsTest(TestCase):
    added = False
    testCount = 1
    
    def setUp(self):
        print "\nFriends Test" + str(FriendsTest.testCount) + "\n"
        if (FriendsTest.added):
            return
        #adding some users to the system
        user1 = User.objects.create_user("Tomer", "t@t.com", "1234")
        user1.save()
        RSMUser(user=user1).save()
        user2 = User.objects.create_user("Eran", "t@t.com", "1234") 
        user2.save()
        RSMUser(user=user2).save()
        user3 = User.objects.create_user("Alex", "t@t.com", "1234")
        user3.save()
        RSMUser(user=user3).save()
        # FriendsTest.added = True
                
    def tearDown(self):
        print "\nEnd Friends Test" + str(FriendsTest.testCount) + "\n"
        FriendsTest.testCount += 1
        
    def test_1_basic(self):
        print "Adding users and checking initial state"
        #Checking init of test
        userNameList = getUserNames(RSMUser.objects.all())
        
        #Asserting users
        assert "Tomer" in userNameList
        assert "Eran" in userNameList
        assert "Alex" in userNameList
        assert "BlaBla" not in userNameList
        
        #Printing users
        for u in userNameList:
            print "Added RSMUser " + str(u) + " to the system"
        
        #Check no one has any friends
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.friends.all())
            self.assertEquals(n,0)

        #Test Finnished
        print "Added users to the system, none have friends"

        
    def test_2_addFriend(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":"Eran"})
        print "Added Eran as friend"
        #Check no one has any friends
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.friends.all())
            if (u.user.username != "Tomer"):
                self.assertEquals(n,0)
            else:
                self.assertEquals(n,1)
        print "Tomer has 1 Friend, No one else has friends"        
    
    
    
    def test_3_removeFriend(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":"Eran"})
        print "Added Eran as friend"
        self.client.post("/friends/remove/", {"friendsList":"Eran"})
        print "Removed Eran from friends"
        #Check no one has any friends
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.friends.all())
            self.assertEquals(n,0)
        print "No one has friends"                
        
        
    def test_4_addMultipleFriends(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":("Eran","Alex")})
        print "Added Eran as friend"
        print "Added Alex as friend"
        #Check no one has any friends
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.friends.all())
            if (u.user.username != "Tomer"):
                self.assertEquals(n,0)
            else:
                self.assertEquals(n,2)
        print "Tomer has 2 Friends, No one else has friends"          

    def test_5_removeMultipleFriends(self):
        userNameList = getUserNames(RSMUser.objects.all())
        assert "Tomer" in userNameList
        self.client.login(password=u"1234",username="Tomer")
        print "Logged in as Tomer"
        self.client.post("/friends/activeAdd/", {"friendsToAdd":("Eran","Alex")})
        print "Added Eran as friend"
        print "Added Alex as friend"
        self.client.post("/friends/remove/", {"friendsList":("Eran", "Alex")})

        #Check no one has any friends
        users = RSMUser.objects.all()
        for u in (users):
            n = len(u.friends.all())
            self.assertEquals(n,0)

        print "Tomer has 2 Friends, No one else has friends"   